package com.xingxue.service;

import java.util.Date;

import com.xingxue.entity.User;
import com.xingxue.util.JdbcUtil;

public class UserServiceImpl implements UserService {

	@Override
	public User findUserBy(String email , String password) {
		String sql = "select email,password,token,expire from demo_autologin_user where email=? and password=?";		
		return JdbcUtil.queryOne(User.class, sql, email, password);
	}
	
	@Override
	public void updateToken(String email, String token, int expireTime) { 
		// 当前时间加上过期的时间
		Date expire = new Date(expireTime * 1000 + System.currentTimeMillis());
		String sql = "update demo_autologin_user set token=?, expire=? where email=?";
		JdbcUtil.update(sql, token, expire, email);
	}

	@Override
	public boolean isTokenValid(String email, String token) {
		// 当前时间，当前时间需要小于数据库中的过期时间
		Date expire = new Date();
		String sql = "select email from demo_autologin_user where email=? and token=? and expire>?";
		User user = JdbcUtil.queryOne(User.class, sql, email, token, expire);
		return user != null;
	}

}
